from itertools import permutations as pm
"""
思路一
若使用之前的暴力组合法  
    n = 5*5*5*5*5 = 3125
    m = 5
    使用组合公式 计算结果 n! / ((n - m)! * m!) = 2475588476563125 
    太大了

思路二
将5个列表分别全排列： 5 * 5！ 个
    再根据 #蓝色在 index = 1
          #挪威在 index = 0
          #喝牛奶的在中间
    来筛选相应的列表
    遍历这五个列表，加入一个二维列表中，每行是各种类型
    再根据剩下的12条线索 得出结果
    
"""
colors = ["red", "white", "blue", "yellow", "green"]
countries = ["Britisher", "Swede", "Danes", "Norwegian", "German"]
drinks = ["tea", "coffee", "milk", "beer", "water"]
smokes = ["PALL MALL", "DUNHILL", "BLENDS", "BLUEMASTER", "PRINCE"]
pets = ["dog", "bird", "cat", "fish", "horse"]
index_color, index_country, index_drink, index_smoke, index_pet = 0, 1, 2, 3, 4
people_len = len(countries)

def judge(data):
    #英国人住红房子
    i = data[index_color].index("red")
    if data[index_country][i] != "Britisher":
        return False
    #瑞典人养狗
    i = data[index_pet].index("dog")
    if data[index_country][i] != "Swede":
        return False
    #丹麦人喝茶
    i = data[index_drink].index("tea")
    if data[index_country][i] != "Danes":
         return False
    #绿房子挨着白房子,在左手边
    i = data[index_color].index("green")
    if i == 4:#绿色在最后 排除
        return False
    if data[index_color][i+1] != "white":
        return False
    #绿房子的主人喝咖啡
    i = data[index_color].index("green")
    if data[index_drink][i] != "coffee":
        return False
    #抽P M 的人养鸟
    i = data[index_smoke].index("PALL MALL")
    if data[index_pet][i] != "bird":
        return False
    #黄屋人抽D 烟
    i = data[index_color].index("yellow")
    if data[index_smoke][i] != "DUNHILL":
        return False
    #抽B 的人 和 养猫的相邻
    i = data[index_smoke].index("BLENDS")
    j = data[index_pet].index("cat")
    if i - j != 1 and i - j != -1:
        return False
    #养马的人和 抽Dun 的人相邻
    i = data[index_pet].index("horse")
    j = data[index_smoke].index("DUNHILL")
    if i - j != 1 and i - j != -1:
        return False
    #抽BLU的人喝啤酒
    i = data[index_smoke].index("BLUEMASTER")
    if data[index_drink][i] != "beer":
        return False
    #德国人抽prin
    i = data[index_country].index("German")
    if data[index_smoke][i] != "PRINCE":
        return False
    #抽BLE的人 和 喝矿泉水的人相邻
    i = data[index_smoke].index("BLENDS")
    j = data[index_drink].index("water")
    if i - j != 1 and i - j != -1:
        return False
    return True

if __name__ == '__main__':
    color_list = list(pm(colors, 5))
    country_list = list(pm(countries, 5))
    drink_list = list(pm(drinks, 5))
    smoke_list = list(pm(smokes, 5))
    pet_list = list(pm(pets, 5))

    _color_list = []
    for color in color_list:
        if color[1] == "blue": #蓝色在 index = 1
            _color_list.append(color)
    _country_list = []
    for country in country_list:
        if country[0] == "Norwegian": #挪威在 index = 0
            _country_list.append(country)
    _drink_list = []
    for drink in drink_list:
        _idx = int(people_len/2)
        if drink[_idx] == "milk": #喝牛奶的在中间
            _drink_list.append(drink)
    _smoke_list = []
    for smoke in smoke_list:
        _smoke_list.append(smoke)
    _pet_list = []
    for pet in pet_list:
        _pet_list.append(pet)
    data = []
    #将所有情况组合起来， 加入线索判断
    for color in _color_list:
        for country in _country_list:
            for drink in _drink_list:
                for smoke in _smoke_list:
                    for pet in _pet_list:
                        data = [color, country, drink, smoke, pet]
                        if judge(data):
                            fish = data[index_pet].index("fish")
                            country = data[index_country][fish]
                            print(country)
                            exit()